import java.util.Arrays;

public class arrTest {
    public static void main(String[] args) {
        //将指定值赋给指定数组的指定范围的每个元素
        int[] array = {1,4,67,9,6};
        int[] array2 = new int[10];
        Arrays.fill(array,-1);
        Arrays.fill(array2,2,6,-1);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));
    }

    public static void main8(String[] args) {
        //比较两个数组是否相同
        int[] array = {1,3,5,7};
        int[] array2 = array;
        boolean flg = Arrays.equals(array,array2);
        System.out.println(flg);
    }
    //数组逆序
    public static void reverse(int[] array) {
        int left = 0;
        int right = array.length-1;
        while(left < right) {
            int tmp = array[left];
            array[left] = array[right];
            array[right]  = array[tmp];
            left++;
            right--;
        }
    }

    public static void main7(String[] args) {
        int[] array = new int[]{1,2,3,4,5};
        reverse(array);
        System.out.println(Arrays.toString(array));
    }
    public static void main6(String[] args) {
        //Arrays中的binarySearch方法
        int[] array = {1,2,4,5,7};
        int ret = Arrays.binarySearch(array,4);
        System.out.println(ret);
    }


    public static void main5(String[] args) {
        //
        int[] array = {1,34,6,7,9};
        Arrays.sort(array);//底层是快速排序
        System.out.println(Arrays.toString(array));
    }

    //查找数组中指定元素
    //法1（直接查找）
    public static int find(int[] array,int key) {
        for (int i = 0; i < array.length; i++) {
            if(array[i] == key)
                return i;
        }
        return -1;//找不到返回-1
    }
    //法2（二分查找）
    public static int binarySearch(int[] array,int key) {
        int left = 0;
        int right = array.length - 1;
        while (left <= right) {
            int mid = (left + right) >>> 1;//无符号右移等效除以2
            if (array[mid] < key) {
                left = mid + 1;
            } else if (array[mid] > key) {
                right = mid + 1;
            } else {
                return mid;
            }
        }
        return -1;
    }

    public static void main4(String[] args) {

        int[] array = {1,4,7,5,9,12};
        System.out.println(find(array, 4));
    }

    public static void main3(String[] args) {
        //求数组的平均值
        int[] array = {1,3,5,7};
        int sum = 0;
        //法1
        for (int i = 0; i < array.length; i++) {
            sum += array[i];
        }
        //法2(foreach)
        for(int x : array) {
            sum += x;
        }
        System.out.println(sum * 1.0 / array.length);
    }



    public static void main2(String[] args) {
        //克隆字符串
        int[] array = new int[]{1,2,3,4,5,6};
        int[] array2 = {1,3,5,7};
        int[] array3 = array2.clone();
        System.out.println(Arrays.toString(array2));
        System.out.println(Arrays.toString(array));
    }


    public static void main1(String[] args) {
        //字符串局部拷贝
        int[] array = {1,2,3,4,5,6};
        int[] array2 = Arrays.copyOfRange(array,2,5);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(array2));

    }
}
